package com.panopset.games.craps;

import com.panopset.centralen.util.MathUtil;

/**
 *
 * Represents two six sided dice.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Dice {

    /**
     * Minumum roll is 2.
     */
    public static final int MIN_ROLL = 2;

    /**
     * Most likely roll is a 7.
     */
    public static final int MOST_LIKELY_ROLL = 7;

    /**
     * Maximum roll is 12.
     */
    public static final int MAX_ROLL = 12;

    /**
     * 6 sided dice are used in craps.
     */
    public static final int SIDES = 6;

    /**
     * Prevent instantiation.
     */
    public Dice() {
        d0 = MathUtil.random(1, Dice.SIDES);
        d1 = MathUtil.random(1, Dice.SIDES);
        setLastRoll(this);
    }

    /**
     * Roll will create a new Dice Object, available from the static
     * getLastRoll method.
     * @return Number between 2 and 12, adding two random numbers between
     * 1 and 6.
     */
    public static int roll() {
        return new Dice().getValue();
    }

    /**
     * Last roll.
     */
    private static Dice lastRoll;

    /**
     * @return Last roll, never null (meaning dice will be rolled if necessary).
     */
    public static Dice getLastRoll() {
        if (lastRoll == null) {
            lastRoll = new Dice();
        }
        return lastRoll;
    }

    /**
     * Set last roll.
     * @param roll Roll.
     */
    public static void setLastRoll(final Dice roll) {
        lastRoll = roll;
    }

    /**
     * First die value.
     */
    private final int d0;

    /**
     * Second die value.
     */
    private final int d1;

    /**
     * @return First die value;
     */
    public int getFirstDieValue() {
        return d0;
    }

    /**
     * @return Second die value;
     */
    public int getSecondDieValue() {
        return d1;
    }

    /**
     * @return First die value plus second die value.
     */
    public int getValue() {
        return d0 + d1;
    }

}
